package exerise_28;

import java.util.Arrays;

public class Exerise_28_InsertSort {
    public static void main(String[] args) {
        int array[] = new int[10];
        int i,j;

        for ( i = 0; i < array.length; i++) {
            array[i] = (int) (Math.random() * 100);
            for ( j = 0; j < i; j++) {
                if (array[i] == array[j]) {
                    i--;
                    break;
                }
            }
        }
        System.out.print("原数组");
        System.out.println(Arrays.toString(array));
        System.out.print("排序好的数组");
        for (i = 1; i < array.length; i++) {
            int temp = array[i];
//            利用循环查找插入位置：到头j=-1
            for ( j = i - 1; j >= 0; j--) {
                if (temp < array[j]) {
                    array[j + 1] = array[j];
                    //[j]->[j+1]向后移动
                }
//                temp不小于ary[j]
                else break;
            }
            array[j+ 1] = temp;//插入temp->[j+1]

        }
        System.out.println(Arrays.toString(array));

    }
}
